import { defineConfig, loadEnv, type ConfigEnv } from 'vite'
import { resolve } from 'path'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import postCssPxToRem from 'postcss-pxtorem'
// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd())
  return {
    plugins: [vue(), vueJsx()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
      extensions: ['.ts', '.vue', '.js', '.jsx', '.tsx'], // 导入时想要省略的扩展名列表。
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove()
                }
              },
            },
          },
          postCssPxToRem({
            rootValue: 3.75, // 1rem等于多少px
            unitPrecision: 3, // 精确到小数点几位
            propList: ['*'], // 哪些属性要转rem单位
            selectorBlackList: [], // 哪些属性不要转rem单位
          }),
        ],
      },
    },
    server: {
      proxy: {
        '/app-dev': {
          target: 'https://m.you.163.com/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/app-dev/, ''),
        },
      },
    },
  }
})
